import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		roles: [],
		// 总数
		total: 0,
	},
	mutations: {
		// 增加对应角色的命座数
		addConstellation(state, name) {
			const index = state.roles.findIndex((item) => item.name == name);
			state.roles[index].Constellation++;
		},

		// 减少对应角色的命座数
		reduceConstellation(state, name) {
			const index = state.roles.findIndex((item) => item.name == name);
			state.roles[index].Constellation--;
		},
	},
	getters: {
		// 计算角色的总年龄数
		totalAge(state) {
			return state.roles.reduce((total, item) => {
				// console.log(item, item.age, total);

				return item.age * item.Constellation + total;
			}, 0);
		},

		// 计算角色总数量
		totalOrders(state) {
			return state.roles.reduce((total, item) => {
				return item.Constellation + total;
			}, 0);
		},
	},
	modules: {},
	actions: {},
});

export default store;
